<?php
	include "include/common.inc.php";

    $errorMsg="";
	$currUser = getLoginUser();
	
	if($currUser->getValue("id") == -1)
		goToUrl("index.php");
		
	if(isset($_POST["Update"])){
			$user = User::getUserByEmail($_POST["email"]);
			if($_POST["password"]!=$_POST["CPassword"])
			$errorMsg="Password not consistent!";
			else if( $_POST["email"]!= $currUser->getValue("email") and $user->getValue("id") != -1)
			$errorMsg="User email already exists! select another one!";
			else
			{
			$city = City::getCity(addslashes($_POST["nation"]), addslashes($_POST["state"]));
			
			
			if($_POST["password"] != "")
				$user = new User(array(
							"id" => $currUser->getValue("id"),
							"type" => $currUser->getValue("type"),
							"firstName" => $_POST["firstname"],
							"lastName" => $_POST["lastname"],
							"email" => $_POST["email"],
							"gender" => $_POST["gender"],
							"password" => password($_POST["password"]),
							"birthday" => storeDate($_POST["birthday"]),
							"cityId" => $city->getValue("id")
							));
			else
				$user = new User(array(
							"id" => $currUser->getValue("id"),
							"type" => $currUser->getValue("type"),
							"firstName" => $_POST["firstname"],
							"lastName" => $_POST["lastname"],
							"email" => $_POST["email"],
							"gender" => $_POST["gender"],
							"birthday" => storeDate($_POST["birthday"]),
							"cityId" => $city->getValue("id")
							));
							
					
					//print"<h2>{$user->getValue("email")}</h2>";
			$user->update();	
			$currUser = getLoginUser();	
			//print"<h2>{$currUser->getValue("email")}</h2>";
			goToUrl("Myprofile.php");
	}

	}
	
?>

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Blind Dating</title>
	<link rel="shortcut icon" href="images/favicon.ico" type="image/x-icon">
    <!-- Bootstrap -->
    <link href="http://getbootstrap.com/dist/css/bootstrap.min.css" rel="stylesheet">
    <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
      <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
      <script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
	<script type= "text/javascript" src = "js/countries2.js"></script>
  </head>
  <body>
  
  <nav class="navbar navbar-default " role="navigation">
  <div class="container-fluid">
    <!-- Brand and toggle get grouped for better mobile display -->
    <div class="navbar-header" >
      <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
        <span class="sr-only">Toggle navigation</span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
      <a class="navbar-brand" href="./index.php">NUS Blind Dating Site</a>
    </div>

    <!-- Collect the nav links, forms, and other content for toggling -->
    <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
      <ul class="nav navbar-nav">
        <li><a href="./index.php">Home</a></li>
        <li><a href="About.php">About</a></li>
        <li><a href="groups.php">Groups</a></li>
	
		
      
      </ul>
      <form class="navbar-form navbar-right" role="search" action="search_group.php" method="get">
        <div class="form-group">
          <input name ="search" type="text" class="form-control" placeholder="Search Group">
        </div>
        <button type="submit" class="btn btn-default" name="submit" value="submit">Search</button>
      </form>
	  <ul class="nav navbar-nav navbar-right">
	  <?php if ($user->getValue("id")==-1) {?>
        <li><a href="Register.php">Register</a></li>
        <li><a href="Login.php">Login</a></li>
      
	  <?php }else{ ?>
	  
	   <li class="dropdown">
          <a href="#" class="dropdown-toggle" data-toggle="dropdown">Hello! <?php echo $user->getValue("userName"); ?> <b class="caret"></b></a>
          <ul class="dropdown-menu">
		  <li><a href="Myprofile.php">View My Profile</a></li>
		<li><a href="Editprofile.php">Edit My Profile</a></li>
        <li><a href="Groups.php">SeeGroup</a></li>
		<li><a href="Logout.php">Logout</a></li>
          </ul>
		  
        </li>
	  <?php }?>
	  </ul>
    </div><!-- /.navbar-collapse -->
  </div><!-- /.container-fluid -->
</nav>


<div class="container" align="center">
<div class="error"><?php echo $errorMsg; ?></div>
  <h2 class="form-signin-heading">Please Update your profile here</h2>
  <form action="" method ="post" name="register",class="form-signin" role="form" >
    <table>
	<tr>
        <td><label>FirstName</label></td>
        <td><input type="text" class="form-control" placeholder="FirstName" required="" autofocus name="firstname" value="<?php echo $currUser->getValue("firstName"); ?>"></td>
      </tr>
	  <tr>
        <td><label>LastName</label></td>
        <td><input type="text" class="form-control" placeholder="LastName" required="" autofocus name="lastname" value="<?php echo $currUser->getValue("lastName"); ?>" ></td>
      </tr>
	
	  <tr>
        <td><label>Gender</label></td>
        <td>
		<select name="gender" class="form-control" required autofocus>
		<option value="Male" <?php if($currUser->getValue("gender") == "Male") echo 'selected="selected"' ?> > Male</option>
		<option value="Female" <?php if($currUser->getValue("gender") == "Female") echo 'selected="selected"' ?>> Female</option>
		</select>
		</td>
      </tr>
      <tr>
        <td><label>Email</label></td>
        <td><input type="email" class="form-control" placeholder="Email address" required="" autofocus name="email" value="<?php echo $currUser->getValue("email"); ?>" ></td>
      </tr>
	  
	  <tr>
        <td><label>Nation</label></td>
		<?php $city = City::getCityById($currUser->getValue("cityId")); ?>
        <td><select class="form-control"  autofocus="" onChange="print_state('state',this.value);" id="country" name = "nation" ></select>
		<script language="javascript">print_country("country",'<?php echo $city->getValue("country"); ?>');</script>
		</td>
      </tr>
	  
	  <tr>
	  <td><label>State</label></td>
	  <td><select class="form-control"  autofocus="" name ="state" id = "state"></select>
<script language="javascript">print_state("state",'<?php echo $city->getValue("country"); ?>','<?php echo $city->getValue("name"); ?>');</script>

</td>
	  </tr>
	  
	  <tr>
	  <td><label> Birthday</label> </td>
	  <td><input type="date" class="form-control" placeholder="Birthday"  autofocus="" name="birthday" value="<?php echo $currUser->getValue("birthday"); ?>"></td>
	  </tr>
	  
      <tr>
	    <td><label>New Password</label></td>
        <td><input type="password" class="form-control" placeholder="Password" required="" name="password" ></td>
      </tr>
	  <tr>
	    <td><label>Confirmed New Password</label></td>
        <td><input type="password" class="form-control" placeholder="Confirmed Password" required="" name="CPassword" ></td>
      </tr>

	  <tr>
      <td width= "50%" align= "left"> <button class="btn btn-lg btn-primary btn-block" type="submit" name="Update" value="Update">Update</button></td>
	  <tr>
    </table>
  </form>
</div>
<!-- Bootstrap core JavaScript
		================================================== -->
<!-- Placed at the end of the document so the pages load faster -->
</body>
</html>